package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.*;
import com.example.demo.entity.DTO.*;
import com.example.demo.mapper.*;
import com.example.demo.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-05-31
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Autowired
    // 用户
    private UserMapper userMapper;

    @Autowired
    // 图片
    private ArticleImgMapper articleImgMapper;

    @Autowired
    // 点赞
    private ArticleLikeMapper articleLikeMapper;

    @Autowired
    // 收藏
    private FavoritesMapper favoritesMapper;

    @Autowired
    // 评论
    private ArticleRemarksMapper articleRemarksMapper;

    @Autowired
    // 转发
    private ForwardMapper forwardMapper;

    //模糊查询指定文章内容
    @Override
    public List<ArticleAndUserDTO> findByArticle(String article) {
        //查询条件是模糊查询
        //查询一个集合
        List<Article> articles = articleMapper.findByArticle(article);
        //装备一个list用来保存dto
        ArrayList<ArticleAndUserDTO> articleAndUserDTOS = new ArrayList<>();
        //判断查询数据是否为空
        if(articles!=null){
            //遍历结果
            for(Article article1:articles){
                //new一个dto
                ArticleAndUserDTO articleAndUserDTO = new ArticleAndUserDTO();
                //entity转dto
                BeanUtil.copyProperties(article1, articleAndUserDTO);
                //dto放入集合
                articleAndUserDTOS.add(articleAndUserDTO);
            }
        }
        return articleAndUserDTOS;
    }

    @Override
    public List<ArticleUserDTO> selectArticleAndUserById(Integer userId) {
        return articleMapper.selectArticleAndUserById(userId);
    }

    @Override
    public List<ArticleSelectDTO> findAllByCriteria(Article article, String userName) {
        return getAllArticle(articleMapper.selectByCriteria(article,userName));
    }

    @Override
    public int publish(Article article) {
        // 存入并返回id
        boolean b = articleMapper.insterArticle(article);
        if (!b) {
            return -1;
        }
        return article.getArticleId();
    }

    @Override
    public Article findArticleById(Integer id) {
        return articleMapper.selectArticleById(id);
    }

    // 获取所有文章以及文章的评论、收藏、点赞、转发
    private List<ArticleSelectDTO> getAllArticle(List<Article> articles){
        ArrayList<ArticleSelectDTO> articleDTOS = new ArrayList<>();
        for (Article article : articles) {

            ArticleSelectDTO articleDTO = new ArticleSelectDTO();
            ArticleUserDTO articleUserDTO = new ArticleUserDTO();
            ArticleRemarksDTO articleRemarksDTO = new ArticleRemarksDTO();
            List<ArticleUserDTO> usersDTO = new ArrayList<>();
            List<ArticleImgDTO> articleImgDTO = new ArrayList<>();

            BeanUtil.copyProperties(article, articleDTO);


            // 返回当前用户信息
            ArticleUserDTO userById = getUserById(article.getUserId(),articleUserDTO);
            articleDTO.setArticleUserDTO(userById);

            // 以帖子ID查询帖子图片
            List<ArticleImg> articleImgs = articleImgMapper.selectByArticleId(article.getArticleId());
            // 转 articleImgDTO 然后全部存入 articleDTO
            for (ArticleImg articleImg : articleImgs) {
                ArticleImgDTO articleImgDTO1 = new ArticleImgDTO();
                BeanUtil.copyProperties(articleImg, articleImgDTO1);
                articleImgDTO.add(articleImgDTO1);
            }
            articleDTO.setArticleImgDTO(articleImgDTO);


            // 将点赞的用户信息返回
            List<ArticleLike> articleLikes = articleLikeMapper.selectLikesByArticleId(article.getArticleId());
            for (ArticleLike articleLike : articleLikes) {
                User user = userMapper.selectById(articleLike.getUserId());
                BeanUtil.copyProperties(user, articleUserDTO);
                usersDTO.add(articleUserDTO);
            }
            articleDTO.setLikeUser(usersDTO);

            // 查找帖子相关收藏用户
            List<ArticleUserDTO> favoritesUser = getFavoritesUser(article.getArticleId(), articleUserDTO);
            articleDTO.setFavoritesUser(favoritesUser);

            // 查找帖子相关评论
            List<ArticleRemarksDTO> remarksDTOS1 = getRemarks(article.getArticleId(), articleUserDTO, articleRemarksDTO);
            articleDTO.setRemarks(remarksDTOS1);

            articleDTOS.add(articleDTO);

        }
        return articleDTOS;
    }


    // 以文章ID查询所有收藏用户
    private List<ArticleUserDTO> getFavoritesUser(Integer articleId,ArticleUserDTO articleUserDTO){
        List<Favorites> favorites = favoritesMapper.selectFavoritesByArticleId(articleId);
        List<ArticleUserDTO> usersDTO = new ArrayList<>();
        for (Favorites favorite : favorites) {
            articleUserDTO = getUserById(favorite.getUserId(), articleUserDTO);
            usersDTO.add(articleUserDTO);
        }
        return usersDTO;
    }

    // 查找帖子相关评论
    private List<ArticleRemarksDTO> getRemarks(Integer articleId,ArticleUserDTO articleUserDTO,ArticleRemarksDTO articleRemarksDTO){
        // 根据文章ID查询相关的评论列表
        List<ArticleRemarks> articleRemarksList = articleRemarksMapper.selectRemarksByArticleId(articleId);

        List<ArticleRemarksDTO> remarksDTOS = new ArrayList<>();

        // 遍历评论列表，为每个评论生成对应的DTO对象
        for (ArticleRemarks articleRemark : articleRemarksList) {
            // 根据评论作者ID获取作者信息，更新DTO对象
            articleUserDTO = getUserById(articleRemark.getUserId(), articleUserDTO);
            // 使用BeanUtil进行属性拷贝，简化代码
            BeanUtil.copyProperties(articleRemark, articleRemarksDTO);
            // 由于BeanUtil可能不会自动复制ID和评论内容，因此需要手动设置
            articleRemarksDTO.setRemarksId(articleRemark.getRemarksId());
            articleRemarksDTO.setRemarks(articleRemark.getRemarks());
            // 将处理好的评论DTO添加到结果列表中
            remarksDTOS.add(articleRemarksDTO);
        }
        // 返回处理后的评论DTO列表
        return remarksDTOS;

    }

    // 根据用户ID查询信息
    private ArticleUserDTO getUserById(Integer userId,ArticleUserDTO articleUserDTO){
        User user = userMapper.selectById(userId);
        BeanUtil.copyProperties(user, articleUserDTO);
        return articleUserDTO;
    }

}
